package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Alteration;
import visiopuce.objets.Clone4k;

public class Clone4kDAOImpl extends GenericDAOImpl<Clone4k, BigDecimal> implements Clone4kDAO {

	/** Constructeur priv� */
	private Clone4kDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static Clone4kDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static Clone4kDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new Clone4kDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public ArrayList<Clone4k> getClone4kByBorne(Alteration alteration, long tailleChr, int seuil) {
		Long debut = Math.max(0, alteration.getDebutHg19() - seuil);
		Long fin = Math.min(alteration.getFinHg19() + seuil, tailleChr);
		ArrayList<Clone4k> clones = null;
		String sql = null;
		Query query;
		sql = "SELECT g FROM Clone4k g WHERE g.debutHg19 IS NOT NULL AND g.debutHg19 <= :fin AND g.finHg19 IS NOT NULL AND g.finHg19 >= :debut AND g.chromosome LIKE :chromosome ORDER BY g.debutHg19";
		query = HibernateUtil.getSession().createQuery(sql).setParameter("debut", debut);
		query.setParameter("fin", fin);
		query.setParameter("chromosome", alteration.getChromosome());
		clones = (ArrayList<Clone4k>) query.list();
		return clones;
	}
}